import java.awt.Checkbox;
import java.awt.Color;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import users.Student;
import client.StudentClient;

import common.ChatIF;



class StudentGUI extends JFrame{
	
	StudentClient stuClient;
	
	
	public StudentGUI(String host, int port, ChatIF professorUI, Object user){
		
		try {
			stuClient= new StudentClient(host, port, professorUI);
			stuClient.user = (Student)user;
			System.out.println("in GUI constructor : " + stuClient.user);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		JTabbedPane jtp = new JTabbedPane();
		
		
		RegisterInformPanel tp = new RegisterInformPanel(stuClient);
		ComputerTimeTablePanel cp = new ComputerTimeTablePanel(stuClient);
	
		
		
		jtp.addTab("스케줄 입력", tp);
		jtp.addTab("학부시험일정", cp);
		
		getContentPane().add(jtp);
		
		
		
		
		setTitle("Student");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(1000,800);
		setVisible(true);
		
	}
}

/////////////////////////////////////////////////////////////////////////////////////////////
class ComputerTimeTablePanel extends JPanel implements ActionListener{
	
	private JTable jTable1;


	public TableModel jTable1Model;
	
	
	//public TextArea display = new TextArea("");
	
	static int row, col;
	
	
	JButton testperiod=new JButton("학부시험일정");
	

	StudentClient stuClient;
	
	
	
	
	// 시간표 
	
	
	public ComputerTimeTablePanel(StudentClient stuClient){
		
		this.stuClient = stuClient;
		
		setPanel();
		initGUI();
		setButton();
									
	}
	
	
	public void actionPerformed(ActionEvent ae){                
		
		String str = ae.getActionCommand();
		String startDate;
		String []classCode = new String[6];
		String []a1 = new String[6];
		String []b1 = new String[6];
		int i;
		int []a = new int[6];
		int []b = new int[6];
		if(str.equals("학부시험일정")){
			stuClient.requireDesidedDay();
			stuClient.requireStartTest();                 //스타트 시간 요청
			
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
			startDate = stuClient.getStartDate();             //시작 날짜 받아옴
			
			
			i=Integer.parseInt(startDate);
			
			for(int j=1; j<6; j++)
			{
				jTable1Model.setValueAt(i+"일", 0, j);
				i++;
			}
			
			
			
			
			classCode = stuClient.getClassCode();
			a1 = stuClient.getRow();
			
			b1 = stuClient.getCol();
			
			
			a[0]=Integer.parseInt(a1[0]);
			b[0]=Integer.parseInt(b1[0]);
			a[1]=Integer.parseInt(a1[1]);
			b[1]=Integer.parseInt(b1[1]);
			a[2]=Integer.parseInt(a1[2]);
			b[2]=Integer.parseInt(b1[2]);
			a[3]=Integer.parseInt(a1[3]);
			b[3]=Integer.parseInt(b1[3]);
			a[4]=Integer.parseInt(a1[4]);
			b[4]=Integer.parseInt(b1[4]);
			a[5]=Integer.parseInt(a1[5]);
			b[5]=Integer.parseInt(b1[5]);
			
			jTable1Model.setValueAt(classCode[0],a[0] ,b[0] );
			jTable1Model.setValueAt(classCode[1],a[1] ,b[1] );
			jTable1Model.setValueAt(classCode[2],a[2] ,b[2] );
			jTable1Model.setValueAt(classCode[3],a[3] ,b[3] );
			jTable1Model.setValueAt(classCode[4],a[4] ,b[4] );
			jTable1Model.setValueAt(classCode[5],a[5] ,b[5] );
		//	display.setText("시간표가 업데이트 되었습니다.\n\n좌측 원하는 시간을 선택 후\n스케줄을 입력하시고\n임시저장 하시면 됩니다.\n\n입력이 끝난 후 에는 완료버튼을 눌러서\n서버에게 알려야만 합니다.");
		//	display.setText(ta.getImpossibleTime());*/
		
		}
		
		else{
			System.exit(0);
		}
	}
	
	
	
	public void setPanel()
	{
		setLayout(null);
		setBackground(Color.white);
	 
	}
	
	private void initGUI() {
		try {
//			setPreferredSize(new Dimension(400, 300));
			{
				jTable1Model = 
						new DefaultTableModel(
								new String[][] { {"","MON", "TUS", "WED", "THU", "FRI"}, 
										{"1A","", "", "", "", ""},
										{"1B","", "", "", "", ""},
										{"2A","", "", "", "", ""},
										{"2B","", "", "", "", ""},
										{"3A","", "", "", "", ""},
										{"3B","", "", "", "", ""},
										{"4A","", "", "", "", ""},
										{"4B","", "", "", "", ""},
										{"5A","", "", "", "", ""},
										{"5B","", "", "", "", ""},
										{"6A","", "", "", "", ""},
										{"6B","", "", "", "", ""},
										{"7A","", "", "", "", ""},
										{"7B","", "", "", "", ""},
										{"8A","", "", "", "", ""},
										{"8B","", "", "", "", ""},
										{"9A","", "", "", "", ""},
										{"9B","", "", "", "", ""},
										{"10A","", "", "", "", ""},
										{"10B","", "", "", "", ""},
										{"11A","", "", "", "", ""},
										{"11B","", "", "", "", ""},
										{"12A","", "", "", "", ""},
										{"12B","", "", "", "", ""},
										{"13A","", "", "", "", ""},
										{"13B","", "", "", "", ""},
										
										
						
								},
								new String[] { "Column 1", "Column 2","3","4","5","6"});
				jTable1 = new JTable();
				this.add(jTable1);
				jTable1.setModel(jTable1Model);
		
				jTable1.setBounds(30,50,900,600);
			
				jTable1.setRowHeight(22);
				jTable1.getColumnModel().getColumn(1).setWidth(40);
				
			
				jTable1.addMouseListener(new MouseAdapter() {
					 public void mouseClicked(MouseEvent evt) {
					         row = jTable1.rowAtPoint(evt.getPoint());
					         col = jTable1.columnAtPoint(evt.getPoint());
					        if (row >= 0 && col >= 0) {
					       
					        }
					
				}});
					
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	public void setButton()
	{
	
		
		
		
		
		testperiod.addActionListener(this);
		
		
	//	display.setEditable(false);          //일단은 수정못하게 

		
		
		//add(jTable1);
	
		//add(display);
		add(testperiod);	  
		 
	
		//  display.setBounds(650, 50, 300 , 250);
		
		  testperiod.setBounds(350, 10, 300, 30);
	
		 
		  
	  
	}

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class RegisterInformPanel extends JPanel implements ActionListener{

	
	
	
	private JTable jTable1;


	public TableModel jTable1Model;
	
	
	public TextArea display = new TextArea("");
	
	static int row, col;
	
	
	JButton testperiod=new JButton("시험기간받아오기");
	
	JLabel jl3= new JLabel("교과목 번호.");
	TextField t2 = new TextField("");
	Checkbox jrb1 = new Checkbox("TEST");
	Checkbox jrb2 = new Checkbox("CLASS");
	
	
	JButton save=new JButton("임시저장");
	JButton modify=new JButton("수정");
	JButton delete=new JButton("삭제");
	JButton complete=new JButton("완료");
	// StudentClient sc = new StudentClient(여기에 로그인 정보);
	StudentClient stuClient;
	
	
	
	
	// 시간표 
	
	
	public RegisterInformPanel(StudentClient stuClient){
		
		this.stuClient = stuClient;
		
		setPanel();
		initGUI();
		setButton();
									
	}
	
	
	public void actionPerformed(ActionEvent ae){                
		
		String str = ae.getActionCommand();
		String startDate;
		int i;
		
		
		if(str.equals("시험기간받아오기")){
			
			stuClient.requireStartTest();
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
			startDate = stuClient.getStartDate();
			i=Integer.parseInt(startDate);
			
			for(int j=1; j<6; j++)
			{
				jTable1Model.setValueAt(i+"일", 0, j);
				i++;
			}
			display.setText("시간표가 업데이트 되었습니다.\n\n좌측 원하는 시간을 선택 후\n스케줄을 입력하시고\n임시저장 하시면 됩니다.\n\n입력이 끝난 후 에는 완료버튼을 눌러서\n서버에게 알려야만 합니다.");
		//	display.setText(ta.getImpossibleTime());*/
		
		}
		
		else if(str.equals("임시저장")){
			if(jrb1.getState()){
			System.out.println(row +","+col);
			stuClient.testNumOfSubject(row, col, t2.getText(),"T");
			jTable1Model.setValueAt(t2.getText()+", T", row, col);
			display.setText(row+","+col+" test 로 저장이 완료되었습니다.");
		//	display.setText(ta.getImpossibleTime());
			}
			else if(jrb2.getState()){
				System.out.println(row +","+col);
				stuClient.classNumOfSubject(row, col, t2.getText(),"C");
				jTable1Model.setValueAt(t2.getText()+", C", row, col);
				display.setText(row+","+col+" class 로 저장이 완료되었습니다.");
			}
		}
		
		else if(str.equals("수정")){
			if(jrb1.getState()){
				System.out.println(row +","+col);
				stuClient.testNumOfSubject(row, col, t2.getText(),"T");
				jTable1Model.setValueAt(t2.getText()+", T", row, col);
				display.setText(row+","+col+" test 로 수정이 완료되었습니다.");
			//	display.setText(ta.getImpossibleTime());
				}
				else if(jrb2.getState()){
					System.out.println(row +","+col);
					stuClient.classNumOfSubject(row, col, t2.getText(),"C");
					jTable1Model.setValueAt(t2.getText()+", C", row, col);
					display.setText(row+","+col+" class 로 수정이 완료되었습니다.");
				}
		}
		
		
		else if(str.equals("삭제")){
			
			stuClient.deleteSubject(row, col);
			jTable1Model.setValueAt("", row, col);
			display.setText(row+","+col+" 가 삭제 되었습니다.");
			
			// ta.setImpossibleTime(t1.getText());    
		
		
			}
		else if(str.equals("완료")){
			stuClient.sendSchedule();
			display.setText("스케쥴 정보 전송이 완료되었습니다.");
			// ta.setImpossibleTime(t1.getText());    
		
		
			}
		else{
			System.exit(0);
		}
	}
	
	
	
	public void setPanel()
	{
		setLayout(null);
		setBackground(Color.white);
	 
	}
	
	private void initGUI() {
		try {
//			setPreferredSize(new Dimension(400, 300));
			{
				jTable1Model = 
						new DefaultTableModel(
								new String[][] { {"","MON", "TUS", "WED", "THU", "FRI"}, 
										{"1A","", "", "", "", ""},
										{"1B","", "", "", "", ""},
										{"2A","", "", "", "", ""},
										{"2B","", "", "", "", ""},
										{"3A","", "", "", "", ""},
										{"3B","", "", "", "", ""},
										{"4A","", "", "", "", ""},
										{"4B","", "", "", "", ""},
										{"5A","", "", "", "", ""},
										{"5B","", "", "", "", ""},
										{"6A","", "", "", "", ""},
										{"6B","", "", "", "", ""},
										{"7A","", "", "", "", ""},
										{"7B","", "", "", "", ""},
										{"8A","", "", "", "", ""},
										{"8B","", "", "", "", ""},
										{"9A","", "", "", "", ""},
										{"9B","", "", "", "", ""},
										{"10A","", "", "", "", ""},
										{"10B","", "", "", "", ""},
										{"11A","", "", "", "", ""},
										{"11B","", "", "", "", ""},
										{"12A","", "", "", "", ""},
										{"12B","", "", "", "", ""},
										{"13A","", "", "", "", ""},
										{"13B","", "", "", "", ""},
										
										
						
								},
								new String[] { "Column 1", "Column 2","3","4","5","6"});
				jTable1 = new JTable();
				this.add(jTable1);
				jTable1.setModel(jTable1Model);
		//		jTable1.setPreferredSize(new java.awt.Dimension(343, 288));
				jTable1.setBounds(30,30,600,600);
			//	
				jTable1.setRowHeight(22);
				jTable1.getColumnModel().getColumn(1).setWidth(20);
				//jTable1.set
			
				jTable1.addMouseListener(new MouseAdapter() {
					 public void mouseClicked(MouseEvent evt) {
					         row = jTable1.rowAtPoint(evt.getPoint());
					         col = jTable1.columnAtPoint(evt.getPoint());
					        if (row >= 0 && col >= 0) {
					        //	System.out.println(row +","+col);
					        }
					
				}});
					
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	public void setButton()
	{
	
		
		
		
		save.addActionListener(this);
		modify.addActionListener(this);
		delete.addActionListener(this);
		testperiod.addActionListener(this);
		complete.addActionListener(this);
		display.setEditable(false);          //일단은 수정못하게 

		
		
		//add(jTable1);
		add(jl3);
		
		
		add(t2);
		
		add(jrb1);
		add(jrb2);
		
		add(display);
	
		
		add(save);
		add(delete);
		add(modify);
		add(testperiod);
		add(complete);
		
		  
		  
		
		
		
		  display.setBounds(650, 50, 300 , 250);
		  
		  
		  jl3.setBounds(700, 350, 100 , 50);
		  t2.setBounds(800, 370, 100 , 20);
		
		  jrb1.setBounds(700, 400, 100, 50);
		  jrb2.setBounds(800, 400, 100, 50);
		  
		  save.setBounds(650, 500, 90, 30);
		  modify.setBounds(750, 500, 80, 30);
		  delete.setBounds(850, 500, 80, 30);
		  
		  testperiod.setBounds(700, 10, 200, 30);
		  complete.setBounds(750, 550, 80, 50);
		 
		  
	  
	}
}

